"""
Case Type   : 系统内部使用工具
Case Name   : 使用help显示gs_probackup add-instance命令的摘要信息
Create At   : 2021/03/30
Owner       : opentestcase015
Description :
    1.执行命令显示gs_probackup add-instance命令的摘要信息
Expect      :
    1.显示gs_probackup add-instance命令的摘要信息成功
History     :
      modeified by opentestcase010 at 2022/3/10:优化提示信息，适配3.0.0新版本
      modified by opentestcase010 at 2022/11/17:优化提示信息，添加回显信息，适配新版本
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.except_msg = '''
        gs_probackup add-instance 
        -B backup-path 
        -D pgdata-path --instance=instance_name
           [-E external-directories-paths]
           [--remote-proto=protocol] [--remote-host=destination]
           [--remote-path=path] [--remote-user=username]
           [--remote-port=port] [--ssh-options=ssh_options]
           [--remote-libpath=libpath]
           [--media-type=type] [--access-id=ak] [--access-key=sk]
           [--access-bucket=bucket] [--endpoint=endpoint] [--region=region]
           [--enable-dss] [--instance-id=instance_id]
           [--vgname="vgdata,vglog"] [--socketpath=socketpath]
        -B, --backup-path=backup-path    
              location of the backup storage area
        -D, --pgdata=pgdata-path         
              location of the database storage area
            --instance=instance_name     name of the new instance
        -E, --external-dirs=external-directories-paths
            backup some directories not from pgdata
            (example: --external-dirs=/tmp/dir1:/tmp/dir2) 
        Remote options:
        --remote-proto=protocol      
        remote protocol to use
         available options: 'ssh', 'none' (default: ssh)
        --remote-host=destination    remote host address or hostname
        --remote-port=port           remote host port (default: 22)
        --remote-path=path           
           path to directory with gs_probackup binary on remote host
                                   (default: current binary path)
        --remote-user=username       
          user name for ssh connection (default: current user)
        --remote-libpath=libpath         library path on remote host
        --ssh-options=ssh_options    
          additional ssh options (default: none)
          (example: --ssh-options='-c cipher_spec -F configfile')
         S3 options:
        --media-type=type            media type for storing backups, including: s3 and disk
        --access-id=ak               access key id, used to identify the user
        --access-key=sk              secret access key, used to verify the user's identity
        --access-bucket=bucket       name of the bucket created on the object storage
        --endpoint=endpoint          accessing domain name which can be in the form of IP: port
        --region=region              geographical region, optional parameters
        DSS options:
        --enable-dss                 enable shared storage mode
        --vgname="vgdata, vglog"        name of dss volume group
        --instance-id=instance_id    instance id of backup node
        --socketpath=socketpath      dss connect socket file path
          '''

    def test_system_internal_tools(self):
        self.log .info('------------------显示帮助信息------------------')
        check_cmd1 = f'''source {macro.DB_ENV_PATH};
        gs_probackup help add-instance;
        '''
        self.log .info(check_cmd1)
        help_msg = self.PrimaryNode.sh(check_cmd1).result()
        self.log .info(help_msg)
        line_msg = help_msg.splitlines()
        self.log .info(line_msg)
        for line in line_msg:
            if line:
                for part in line.split():
                    self.assertIn(part.strip(), self.except_msg)

    def tearDown(self):
        # 无须清理环境
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

